﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sasc.PV.BLL;
using Sasc.PV.DAL;

namespace Sasc.PV.Web.Admin.SystemManage
{
    public partial class SystemLog : PVAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBindToGridview();

                //设定按钮权限
                DeleteImageButton.Visible = PublicMethod.HasPermission("|087D|");
                ReportImageButton.Visible = PublicMethod.HasPermission("|087E|");
            }
        }

        //查询
        protected void SearchImageButton_Click(object sender, ImageClickEventArgs e)
        {
            DataBindToGridview();
        }
        //删除
        protected void DeleteImageButton_Click(object sender, ImageClickEventArgs e)
        {
            string IDlist = PublicMethod.CheckCbx(this.GVData, "CheckSelect", "LabVisible");
            if (DbHelperSQL.ExecuteSQL("delete from ERPRiZhi where ID in (" + IDlist + ")") == -1)
            {
                Response.Write("<script>alert('删除选中记录时发生错误！请重新登陆后重试！');</script>");
            }
            else
            {
                DataBindToGridview();
                //写系统日志
                WriteRiZhi("用户删除角色信息");
            }
        }
        //导出
        protected void ReportImageButton_Click(object sender, ImageClickEventArgs e)
        {

        }

        #region  分页方法
        protected void ButtonGo_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (GoPage.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
                }
                else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GVData.PageCount)
                {
                    Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
                }
                else if (GoPage.Text.Trim() != "")
                {
                    int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                    if (PageI >= 0 && PageI < (GVData.PageCount))
                    {
                        GVData.PageIndex = PageI;
                    }
                }

                if (TxtPageSize.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不可以为空!');</script>");
                }
                else if (TxtPageSize.Text.Trim().ToString() == "0")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                }
                else if (TxtPageSize.Text.Trim() != "")
                {
                    try
                    {
                        int MyPageSize = int.Parse(TxtPageSize.Text.ToString().Trim());
                        this.GVData.PageSize = MyPageSize;
                    }
                    catch
                    {
                        Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                    }
                }

                DataBindToGridview();
            }
            catch
            {
                DataBindToGridview();
                Response.Write("<script language='javascript'>alert('请输入有效数字！');</script>");
            }
        }

   

        protected void PagerButtonClick(object sender, ImageClickEventArgs e)
        {
            //获得Button的参数值
            string arg = ((ImageButton)sender).CommandName.ToString();
            switch (arg)
            {
                case ("Next"):
                    if (this.GVData.PageIndex < (GVData.PageCount - 1))
                        GVData.PageIndex++;
                    break;
                case ("Pre"):
                    if (GVData.PageIndex > 0)
                        GVData.PageIndex--;
                    break;
                case ("Last"):
                    try
                    {
                        GVData.PageIndex = (GVData.PageCount - 1);
                    }
                    catch
                    {
                        GVData.PageIndex = 0;
                    }

                    break;
                default:
                    //本页值
                    GVData.PageIndex = 0;
                    break;
            }
            DataBindToGridview();
        }
        #endregion

        protected void GVData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);

        }
        private void DataBindToGridview()
        {

            ERPRiZhi MyRiZhi = new ERPRiZhi();
            string text = this.TextBox2.Text.Trim();
            string sql = "1=1  ";
            if (text != "")
            {
                sql += " and ";
                string[] temp = text.Split(',');
                for (int i = 0; i < temp.Length; i++)
                {
                    sql += "  UserName Like '%" + temp[i] + "%' or";
                }
                sql = sql.Substring(0, sql.Length - 2);
            }
            else
            {
                sql += "and UserName Like '%" + this.TextBox2.Text.Trim() + "%'";
            }

            sql += " and  DoSomething Like '%" + this.TextBox1.Text.Trim() + "%' ";

            GVData.DataSource = MyRiZhi.GetList(GVData.PageIndex, GVData.PageSize,sql, " ID desc");
            GVData.DataBind();
            LabPageSum.Text = Convert.ToString(GVData.PageCount);
            LabCurrentPage.Text = Convert.ToString(((int)GVData.PageIndex + 1));
            this.GoPage.Text = LabCurrentPage.Text.ToString();
        }


    }
}